﻿#include "UCarouseshowPage.h"
#include "Global.h"
#include "CBaseDevice.h"
#include <QDebug>
#include <QtConcurrent/QtConcurrentRun>
UCarouseshowPage::UCarouseshowPage(CPageCarouse *pPage, QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	m_pPageCarouse = pPage;
	m_pGridLayout = new QGridLayout(this);
	m_pGridLayout->setSpacing(0);
	m_pGridLayout->setContentsMargins(0, 0, 0, 0);
	setAttribute(Qt::WA_StyledBackground, true);
	this->setMinimumSize(400, 300);
	m_bRefush = false;

	QFile filePage(":/SmartControl/Qss/page.qss"); // 资源路径
	if (filePage.open(QFile::ReadOnly))
	{
		m_sPageqssStr = filePage.readAll();
		filePage.close();
	}

	QFile fileCard(":/SmartControl/Qss/card.qss"); // 资源路径
	if (fileCard.open(QFile::ReadOnly))
	{
		m_sCardqssStr = fileCard.readAll();
		fileCard.close();
	}
	//this->setStyleSheet(m_sPageqssStr);
	initUi();
}

UCarouseshowPage::~UCarouseshowPage()
{
	if (m_pGridLayout != nullptr)
	{
		delete m_pGridLayout;
		m_pGridLayout = nullptr;
	}
}

void UCarouseshowPage::refushDate(bool bRefush)
{
	for (int i = 0; i < m_carouse.size(); i++)
	{
		m_carouse[i]->pWidget->update();
	}
	if (bRefush == m_bRefush)
	{
		return;
	}
	m_bRefush = bRefush;
	//QtConcurrent::run([this, bRefush]()
	//	{
	//		for (auto v : m_carouse)
	//		{
	//			QMetaObject::invokeMethod(v->pWidget, "refreshData", Qt::DirectConnection, Q_ARG(bool, bRefush));
	//		}
	//	});
	for (int i = 0; i < m_carouse.size(); i++)
	{
		if (m_carouse[i]->pWidget != NULL)
		{
			QMetaObject::invokeMethod(m_carouse[i]->pWidget, "refreshData", Qt::DirectConnection, Q_ARG(bool, bRefush));
		}		
	}
}

void UCarouseshowPage::initUi()
{
	QVector<SPageItem> vecPageItem = m_pPageCarouse->getPageItems();
	m_carouse.clear();
	m_widgetGridRects.clear();

	// 固定 4x4 伸缩比例
	for (int r = 0; r < m_gridRows; ++r)
		m_pGridLayout->setRowStretch(r, 1);
	for (int c = 0; c < m_gridCols; ++c)
		m_pGridLayout->setColumnStretch(c, 1);

	for (const auto& item : vecPageItem)
	{
		// 校验不能越界
		if (item.nRow < 0 || item.nCol < 0 ||
			item.nRow + item.nRowSpan > m_gridRows ||
			item.nCol + item.nColSpan > m_gridCols)
		{
			qWarning("控件位置 [%d,%d,%d,%d] 越出 4x4 网格，已忽略",
				item.nRow, item.nCol, item.nRowSpan, item.nColSpan);
			continue;
		}

		QWidget* pWidget = getDevWidget(item.sGuid, item.sCardGuid);	
		if (!pWidget) 
		{
			pWidget = new QWidget();
			//pWidget->setStyleSheet("background-color: transparent;");
		}
		addGridWidget(item, pWidget);	
	}
}

void UCarouseshowPage::addGridWidget(SPageItem item, QWidget * pWidget)
{
	//int cellW = 471;
	//int cellH = 226;

	//int w = (cellW + 5) * item.nColSpan - 5;
	//int h = (cellH + 5) * item.nRowSpan - 5;
    int cellW = 480;
	int cellH = 240;

	int w = (cellW ) * item.nColSpan;
	int h = (cellH) * item.nRowSpan;
	pWidget->setFixedSize(w, h);  // 固定大小
	pWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	SCarouseInfo *pCarouse = new SCarouseInfo();
	pCarouse->pWidget = pWidget;
	/*UWidgetRestorer *pRestorer = new UWidgetRestorer(pWidget);
	pCarouse->pRestorer = pRestorer;*/
	pCarouse->nRow = item.nRow;
	pCarouse->nCol = item.nCol;
	pCarouse->nRowSpan = item.nRowSpan;
	pCarouse->nColSpan = item.nColSpan;
	m_widgetGridRects.append(QRect(item.nCol, item.nRow, item.nColSpan, item.nRowSpan)); // 注意X/Y是列/行
	m_pGridLayout->addWidget(pWidget, pCarouse->nRow, pCarouse->nCol, pCarouse->nRowSpan, pCarouse->nColSpan, Qt::AlignCenter);
	m_carouse.push_back(pCarouse);
}

void UCarouseshowPage::resizeEvent(QResizeEvent* event)
{
	//updateGridLayoutSize();
	QWidget::resizeEvent(event);
}

void UCarouseshowPage::showEvent(QShowEvent * event)
{
	/*for (const auto * item : m_carouse)
	{
		item->pWidget->setParent(NULL);
		item->pWidget->setParent(this);
		item->pWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
		m_pGridLayout->addWidget(item->pWidget, item->nRow, item->nCol, item->nRowSpan, item->nColSpan);
		item->pWidget->show();
	}*/

}

void UCarouseshowPage::hideEvent(QHideEvent * event)
{
	/*for (const auto * item : m_carouse)
	{
		item->pRestorer->restore();
	}*/
}


void UCarouseshowPage::updateGridLayoutSize()
{
	QSize totalSize = this->size();
	int cellW = totalSize.width() / m_gridCols;
	int cellH = totalSize.height() / m_gridRows;

	for (int i = 0; i < m_carouse.size(); ++i)
	{
		QWidget* widget = m_carouse[i]->pWidget;
		QRect gridRect = m_widgetGridRects[i]; // col, row, span

		//int w = (cellW+5) * gridRect.width()-5;
		//int h = (cellH+5) * gridRect.height()-5;


		// 必须重新设置，确保不会被 layout 拉伸
		//widget->setMinimumSize(w-20, h-20);
		//widget->setMaximumSize(w-20, h-20);

		int w = (cellW ) * gridRect.width();
		int h = (cellH ) * gridRect.height();
		widget->setMinimumSize(w, h);
		widget->setMaximumSize(w, h);
	}
}

QWidget * UCarouseshowPage::getDevWidget(QString sGuid, QString sCardGuid)
{
	QWidget *pWidget = nullptr;
	CBaseDevice *pDevice = (CBaseDevice *)findNodeByGuid(sGuid);
	if (pDevice != nullptr)
	{
		pWidget = pDevice->getDevice()->getDeviceThumbnailUi(sCardGuid.toInt());
		QString sMyqss = pWidget->styleSheet();
		QString merged = m_sCardqssStr + "\n" + sMyqss; // 后面的会覆盖前面的相同规则
		pWidget->setAttribute(Qt::WA_StyledBackground, true);
		pWidget->setStyleSheet(merged);
		//pWidget->setParent(this);
	}
	return pWidget;
}
